package hk.hku.hooku.pojo;

import hk.hku.hooku.util.DateEntryComparator;
import hk.hku.hooku.util.TimeUtil;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map.Entry;

public class Event {
	private String url;
	private String title;
	private String location;
	private String description;
	private User proposer;
	private int status;
	private Timestamp createTime;
	private Timestamp closeTime;
	private String proposerEmail;
	private ArrayList<EventChoice> eventChoices;
	private EventResult eventResult;
	private ArrayList<EventVote> eventVotes;

	public Event() {
		super();
		this.eventChoices = new ArrayList<EventChoice>();
		this.eventVotes = new ArrayList<EventVote>();
	}

	public Event(String url, String title, String location, String description,
			User proposer, int status, Timestamp createTime,
			Timestamp closeTime, String proposerEmail,
			ArrayList<EventChoice> eventChoices) {
		super();
		this.url = url;
		this.title = title;
		this.location = location;
		this.description = description;
		this.proposer = proposer;
		this.status = status;
		this.createTime = createTime;
		this.closeTime = closeTime;
		this.proposerEmail = proposerEmail;
		this.eventChoices = eventChoices;
		this.eventVotes = new ArrayList<EventVote>();
	}

	public Event(String url, String title, String location, String description,
			User proposer, int status, Timestamp createTime,
			Timestamp closeTime, String proposerEmail,
			ArrayList<EventChoice> eventChoices, EventResult eventResult,
			ArrayList<EventVote> eventVotes) {
		super();
		this.url = url;
		this.title = title;
		this.location = location;
		this.description = description;
		this.proposer = proposer;
		this.status = status;
		this.createTime = createTime;
		this.closeTime = closeTime;
		this.proposerEmail = proposerEmail;
		this.eventChoices = eventChoices;
		this.eventResult = eventResult;
		this.eventVotes = eventVotes;
	}

	public String getUrl() {
		return url;
	}

	public String getTitle() {
		return title;
	}

	public String getLocation() {
		return location;
	}

	public String getDescription() {
		return description;
	}

	public User getProposer() {
		return this.proposer;
	}

	public int getStatus() {
		return status;
	}

	public Timestamp getCreateTime() {
		return createTime;
	}

	public Timestamp getCloseTime() {
		return closeTime;
	}

	public String getProposerEmail() {
		return proposerEmail;
	}

	public EventResult getEventResult() {
		return eventResult;
	}

	public ArrayList<EventVote> getEventVotes() {
		return eventVotes;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setProposer(User proposer) {
		this.proposer = proposer;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public void setCreateTime(Timestamp createTime) {
		this.createTime = createTime;
	}

	public void setCloseTime(Timestamp closeTime) {
		this.closeTime = closeTime;
	}

	public void setProposerEmail(String proposerEmail) {
		this.proposerEmail = proposerEmail;
	}

	public void setEventResult(EventResult eventResult) {
		this.eventResult = eventResult;
	}

	public void setEventVotes(ArrayList<EventVote> eventVotes) {
		this.eventVotes = eventVotes;
	}

	public ArrayList<EventChoice> getEventChoices() {
		return eventChoices;
	}

	public void setEventChoices(ArrayList<EventChoice> eventChoices) {
		this.eventChoices = eventChoices;
	}

	public ArrayList<Entry<Date, ArrayList<EventChoice>>> getSortedeventChoices() {
		HashMap<Date, ArrayList<EventChoice>> choiceMap = new HashMap<Date, ArrayList<EventChoice>>();
		for (EventChoice choice : this.eventChoices) {
			Timestamp startTime = choice.getStartTime();
			Date startDate = TimeUtil.toDate(startTime);
			if (choiceMap.containsKey(startDate)) {
				choiceMap.get(startDate).add(choice);
			} else {
				ArrayList<EventChoice> choices = new ArrayList<EventChoice>();
				choices.add(choice);
				choiceMap.put(startDate, choices);
			}
		}
		ArrayList<Entry<Date, ArrayList<EventChoice>>> sortedChoices = new ArrayList<Entry<Date, ArrayList<EventChoice>>>(
				choiceMap.entrySet());
		Collections.sort(sortedChoices, new DateEntryComparator());
		return sortedChoices;
	}
}
